import os
import logging
import settings


class Logger(object):

    level_relations = {
        'debug': logging.DEBUG,
        'info': logging.INFO,
        'warning': logging.WARNING,
        'error': logging.ERROR,
        'crit': logging.CRITICAL
    }

    def __init__(self):
        log_dir = os.path.dirname(settings.LOG_FILE)
        if not os.path.exists(log_dir):
            os.makedirs(log_dir)
        if os.path.exists(settings.LOG_FILE):
            os.remove(settings.LOG_FILE)
        self.logger = logging.getLogger(settings.LOG_FILE)
        format_str = logging.Formatter(settings.LOG_FORMAT)
        self.logger.setLevel(self.level_relations.get(settings.LOG_LEVEL.lower()))
        
        sh = logging.StreamHandler()
        sh.setFormatter(format_str)
        self.logger.addHandler(sh)
        fh = logging.FileHandler(settings.LOG_FILE, encoding='utf-8')
        fh.setFormatter(format_str)
        self.logger.addHandler(fh)

logger = Logger().logger
logger.info("logger object created. save path: %s" % settings.LOG_FILE)

